package tmc.gui;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;
import org.geotools.map.MapLayer;
import org.geotools.swing.styling.JSimpleStyleDialog;


import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.MultiLineString;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;

import tmc.main.TMCConstants;
import tmc.utils.SpringUtilities;

public class LayerControlPanel extends JPanel {

	private LayerControlPanel layerControlPanel = this;
	private MapLayer layer;
//	private MapContext mapContext;
	private LeftMenuView parent;
	private MapView mapView;

	private JButton deleteButton;
	private JButton styleButton;
	private JButton upButton;
	private JButton downButton;
	private JCheckBox activeCheckBox;
	private JLabel nameLabel;
	private JLabel typeLabel;
	
	private int id;
	

	public LayerControlPanel(LeftMenuView leftMenuView,MapView mv, MapLayer lay,
			MapContext context) {
		
		setLayer(lay);
		mapView = mv;
//		mapContext = context;
		parent = leftMenuView;
		init();
	}
	
	private void init() {

		setSize(new Dimension(TMCConstants.LEFT_MENU_VIEW_HORIZONTAL_SIZE,
				TMCConstants.LAYER_CONTROL_PANEL_VERTICAL_SIZE));
		setLayout(new SpringLayout());

		// Name label
		String label = !"".equals(getLayer().getTitle()) ? getLayer().getTitle() : "Layer id: "+ new Random().nextInt(1000000);
		nameLabel = new JLabel(label);
		nameLabel.setToolTipText("Name of layer");
		add(nameLabel);

		// Content type label
		String contentTypeLabel = "<html> ";

		Class geomType = getLayer().getFeatureSource().getSchema()
				.getGeometryDescriptor().getType().getBinding();

		if (Polygon.class.isAssignableFrom(geomType)
				|| MultiPolygon.class.isAssignableFrom(geomType)) {
			contentTypeLabel += "<font color=red>Pol </font>";

		}
		if (LineString.class.isAssignableFrom(geomType)
				|| MultiLineString.class.isAssignableFrom(geomType)) {
			contentTypeLabel += "<font color = green>Ln </font>";

		}
		if (Point.class.isAssignableFrom(geomType)) {
			contentTypeLabel += "<font color = blue> Pt </font>";
		}
		typeLabel = new JLabel(contentTypeLabel + "</html>");
		typeLabel.setToolTipText("Shape type codes: Pol - polygon, Ln - line, Pt - point");
		add(typeLabel);
		
		// Active Checkbox
		
		activeCheckBox = new JCheckBox("", true);
		activeCheckBox.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent e) {
				if(activeCheckBox.isSelected()){
					getLayer().setVisible(true);
				} else {
					getLayer().setVisible(false);
				}
				mapView.revalidate();
				mapView.repaint();				
			}
		});
		activeCheckBox.setMargin(new Insets(0, 0, 0, 0));
		add(activeCheckBox);
		
		
		
		// Delete Button
		deleteButton = new JButton(TMCConstants.DELETE_ICON);
		deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				parent.remove(layerControlPanel);
				//mapContext.removeLayer(getLayer());
				parent.revalidate();
				parent.repaint();
			}
		});
		deleteButton.setToolTipText("Delete layer");
		deleteButton.setContentAreaFilled(false);	
		deleteButton.setMargin(new Insets(0, 0, 0, 0));
		add(deleteButton);

		// Style button
		styleButton = new JButton(TMCConstants.STYLE_ICON);
		styleButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getLayer().setStyle(JSimpleStyleDialog.showDialog(null, getLayer()));
				mapView.revalidate();
				mapView.repaint();
			}
		});
		styleButton.setContentAreaFilled(false);
		styleButton.setToolTipText("Press to change layer style");
		styleButton.setMargin(new Insets(0, 0, 0, 0));
		add(styleButton);
		
		upButton = new JButton(TMCConstants.MOVE_UP_ICON);
		upButton.addActionListener(new ActionListener() {	
			public void actionPerformed(ActionEvent e) {
				parent.moveUp(layerControlPanel);
			}
		});
		upButton.setToolTipText("Move layer up");
		upButton.setContentAreaFilled(false);
		upButton.setMargin(new Insets(0, 0, 0, 0));
		add(upButton);
		
		downButton = new JButton(TMCConstants.MOVE_DOWN_ICON);
		downButton.addActionListener(new ActionListener() {	
			public void actionPerformed(ActionEvent e) {
				parent.moveDown(layerControlPanel);
			}
		});
		downButton.setToolTipText("Move layer down");
		downButton.setContentAreaFilled(false);
		downButton.setMargin(new Insets(0, 0, 0, 0));
		add(downButton);

		SpringUtilities.makeCompactGrid(this, 1, 7, 0, 0, 2, 0);
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setLayer(MapLayer layer) {
		this.layer = layer;
	}

	public MapLayer getLayer() {
		return layer;
	}

}
